package config;

import com.jt.www.config.convert.ProtostuffHttpMessageConverter;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Author wangyang
 * @Date 2018/10/30 上午11:59
 * @Description 注入使用自己的protostuff编解码器来替换feign默认的json编解码器
 **/
@Configuration
public class ProtoFeignConfiguration {

    @Resource
    private ObjectFactory<HttpMessageConverters> messageConverterObjectFactory;

    @Bean
    public ProtostuffHttpMessageConverter protostuffHttpMessageConverter() {
        return new ProtostuffHttpMessageConverter();
    }

    @Bean(name = "protoEncoder")
    public Encoder springEncoder() {
        return new SpringEncoder(this.messageConverterObjectFactory);
    }

    @Bean(name = "protoDecoder")
    public Decoder springDecoder() {
        return new ResponseEntityDecoder(new SpringDecoder(this.messageConverterObjectFactory));
    }
}


